<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header
      ><div>
        <img src="@/assets/heima.png" alt="电商后台管理系统" />
        <!-- 顶部标题 -->
        <span>&nbsp;&nbsp;&nbsp;电商后台管理系统</span>
      </div>
      <el-button type="danger" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px" :with="isCollapse ? '64px' : '200px'">
        <!-- 菜单组件 -->
        <div class="toggle-button" @click="isCollapse = !isCollapse">📜</div>
        <el-menu
          :default-active="activePath"
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
        >
          <!-- 一级菜单 遍历data中的菜单数组 -->
          <el-submenu
            :index="menu.id + ''"
            :key="menu.id + ''"
            v-for="menu in menuList"
          >
            <!-- 一级菜单内容区域 -->
            <template slot="title">
              <i :class="menu.iconclass"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 遍历 一级菜单.children -->
            <el-menu-item
              @click="changePath('/' + subMenu.path)"
              :key="subMenu.id"
              v-for="subMenu in menu.children"
              :index="'/' + subMenu.path"
            >
              <!-- 二级菜单内容区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subMenu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import { config } from 'vue/types/umd'
import { getMenusAPI } from '@/api'
export default {
  data() {
    return {
      menuList: [], // 菜单数组
      isCollapse: false,
      activePath: '/welcome' //当前激活菜单的path
    }
  },
  methods: {
    logout() {
      if (!confirm('确定退出吗?')) return
      window.sessionStorage.removeItem('token')
      this.$router.replace('/login')
    },
    changePath(aPath) {
      window.sessionStorage.setItem('activePath', aPath)
      this.activePath = aPath
    }
  },
  // 组件入口函数
  async created() {
    // 1.请求菜单数据
    const { data: res } = await getMenusAPI()
    // console.log(res);
    // 2.如果请求失败,则提示错误
    if (res.meta.status !== 200) return this.$msg.error(res.meta.msg)
    // 3.如果请求成功,则将数据 挂载给 data变量
    this.menuList = res.data
    // console.log(this.menuList);
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 7px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 24px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
